$(function () {
    obj.init();
});

var obj = new function() {
    return {
        applyForm: null,
        form: null,
        targetObj: null,
        otherTr: null,
        checkTeacher: true,
        isDefault: true,
        init: function () {
            this.applyForm = $('#reference_back_apply_form');
            this.form = new xhForm(this.applyForm);
            this.form.parse().bindRequired($('#bt_modal_confirm',parent.document));
            this.funObj().addData();//获取select数据
            this.listener();

        },
        listener: function () {
            var $this = this;
            $('#school, #subject').unbind('change').on('change',function () {
                if (modify === 1){
                    $this.funObj().addTeacherData();
                } else {
                    if (modify === 2 && $this.isDefault === false){//修改初始化做请求
                        $this.funObj().addTeacherData();
                    }
                }
            });
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
        },
        funObj: function () {
            var form = this.applyForm;
            var $this = this;
            var teaArr = [];
            var id = id_data;
            return {
                addData: function () {
                    if (modify === 2){//修改
                        params = {type: 3, id: id};
                    } else {//创建
                        params = {};
                    }
                    xhExtra.ajaxRequest('res/getReferAllSelectData', params, {check:true}, function (data) {
                        schoolObj = $('#school').select2({placeholder: '选择学校', data: data.school, allowClear: true});
                        applicationObj = $('#application').select2({placeholder: '选择应用',data: data.app, allowClear: true});
                        classObj = $('#class').select2({placeholder: '选择年级',data: data.class, allowClear: true});
                        subjectObj = $('#subject').select2({placeholder: '选择科目',data: data.subject, allowClear: true});
                        if(modify === 2){//修改
                            schoolObj.val([apply_data.iSchoolId]).trigger('change');
                            applicationObj.val([apply_data.sAppCode]).trigger('change');
                            classObj.val([apply_data.iClassId]).trigger('change');
                            subjectObj.val([apply_data.iSubject]).trigger('change');
                            teacherObj = $('#teacher').select2({placeholder: '选择教师',data: data.allTeaList,allowClear: true,multiple : true});
                            $.each(data.teaList, function (key, value) {
                                teaArr.push(value);
                            });
                            teacherObj.val(teaArr).trigger('change');//填充数据
                            $this.isDefault = false;
                            teaArr = [];
                        }else{//创建
                            var handleData = window.top.xhTop.getActiveFrame().obj.handleData_Back;
                            if (handleData.school !== null){
                                schoolObj.val([handleData.school]).trigger('change');
                            }
                            if (handleData.application !== null){
                                applicationObj.val([handleData.application]).trigger('change');
                            }
                            if (handleData.class_data !== null){
                                classObj.val([handleData.class_data]).trigger('change');
                            }
                            if (handleData.subject !== null){
                                subjectObj.val([handleData.subject]).trigger('change');
                            }
                            if (handleData.teacher !== null){
                                $this.funObj().addTeacherData(false, handleData.teacher);
                            }
                        }

                    });
                },
                addTeacherData: function (exist, existTeacherObject) {//是否是编辑、是否存在操作的教师数据
                    var schoolId = form.find('#school').val();
                    var subjectId = form.find('#subject').val();
                    $("#teacher").find("option").remove();
                    $this.checkTeacher = true;
                    xhExtra.ajaxRequest('res/getTeacherData', {schoolId:schoolId,subjectId:subjectId,checkType:true}, {check:true}, function (data) {
                        if (data.checkTeacher === false){
                            $this.checkTeacher = false;
                        }
                        teacherObj = $('#teacher').select2({placeholder: '选择教师',data: data.data,allowClear: true,multiple : true});
                        if (exist){//编辑
                            $.each(apply_data.sTeacherList, function (key, value) {
                                teaArr.push(value);
                            });
                            teacherObj.val(teaArr).trigger('change');//填充数据
                        }else{//创建
                            if (existTeacherObject !== null && existTeacherObject !== undefined ){
                                $.each(existTeacherObject, function (key, value) {
                                    teaArr.push(value);
                                });
                                teacherObj.val(teaArr).trigger('change');//填充数据
                            }
                        }
                        teaArr = [];
                    });
                },
                saveData: function () {
                    var school = $('#school').val();
                    var application = $('#application').val();
                    var grade = $('#class').val();
                    var subject = $('#subject').val();
                    var teacher = $('#teacher').val();
                    var bookName = $('#bookName').val();
                    var referenceId = $('#referenceId').val();
                    var iId = id;
                    var url = '';
                    if (school < 1 || application < 1 || grade < 1 || subject < 1 || bookName < 1 ){
                        window.top.xhTop.showError('请填写完整信息');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (application === 'CA103001' && (teacher < 1 || teacher.length < 1)){
                        window.top.xhTop.showError('云作业应用请选择老师');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (modify === 1){//创建
                        $this.funObj().fillHandleData(school, application, grade, subject, teacher);
                        url = 'res/saveReferenceRepeal';
                    }else{//修改
                        url = 'res/saveModifyReferRepeal';
                    }
                    xhExtra.ajaxRequest(url, {iId: iId,school: school, application: application,grade: grade,
                            subject: subject,teacher: teacher,referenceId: referenceId,bookName:bookName,remark: $('#remark').val()
                        }, {success: true,check:false},function (res) {
                        if(res.code === 200){
                            window.top.xhTop.showSuccess(res.msg);
                            window.top.xhTop.closeModal();
                        } else {
                            window.top.xhTop.showError(res.error);
                            xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        }
                    });
                },
                fillHandleData: function (school, application, grade, subject, teacher) {
                    handleData = window.top.xhTop.getActiveFrame().obj.handleData_Back;
                    handleData.school = school;
                    handleData.application = application;
                    handleData.class_data = grade;
                    handleData.subject = subject;
                    handleData.teacher = teacher;
                }
            }
        }
    }
};
